<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Cron extends Controller {

    public function action_index() {
        $model = ORM::factory('rookeeaccount')->find();
        $paramsauth["login"] = $model->username; // Логин
        $paramsauth["password"] = $model->password; // Пароль
        $api = new Model_Api($paramsauth);
        $main = new Model_Main();
        $projects = $api->getProjects();
        $regions = $api->getRegions();
        foreach ($regions as $region) {
            $main->update_regions($region->ID, $region->Name);
        }
        foreach ($projects as $project) {
            $region_id = $api->getProjectRegion($project['id']);
            $regions[] = ORM::factory('region')->where("reg_id", "=", $region_id)->find()->name;
            Model::factory('main')->update_projects($project['id'], $project['domain'], $region_id);
            $queries = $api->getQueries($project['id']);
            if (!empty($queries)) {
                foreach ($queries as $query) {
                    Model::factory('Main')->update_queries($query['id'], $project['id'], $query['title'], $query['target']);
                    $position = $api->getPositions($query['id'], date('Y-m-d', date(strtotime('-1 month'))), date('Y-m-d'));
                    foreach ($position as $pos) {
                        Model::factory('Main')->update_positions($query['id'], $pos->Date, $pos->PositionYandex, $pos->PositionGoogle);
                    }
                }
            }
        }
    }

}

?>